package com.kh.job;

import com.kh.job.entity.Job;
import com.kh.job.repository.JobRepository;
import com.kh.job.util.SpringFactory;
import com.vip.saturn.job.AbstractSaturnJavaJob;
import com.vip.saturn.job.SaturnJobExecutionContext;
import com.vip.saturn.job.SaturnJobReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * SpringJob
 *
 * @author crossice
 * @date 2018/7/3
 */
@Service
public class SpringJob extends AbstractSaturnJavaJob {


    @Autowired
    private JobRepository jobRepository;


    @Override
    public SaturnJobReturn handleJavaJob(String jobName, Integer shardItem, String shardParam, SaturnJobExecutionContext shardingContext) throws InterruptedException {



        return handleJavaJob123(jobName, shardItem, shardParam, shardingContext);
    }

    private SaturnJobReturn handleJavaJob123(String jobName, Integer shardItem, String shardParam, SaturnJobExecutionContext shardingContext) throws InterruptedException {

        Job job = new Job();
        job.setName(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        job = jobRepository.save(job);

        System.out.println(job.toString());

        return new SaturnJobReturn("完成Spring测试");
    }

    /**
     * 这是个静态方法，在executor初始化时会调用，并生成供saturn使用的实现类对象
     */
    public static Object getObject() {
        SpringJob instance = (SpringJob) SpringFactory.getInstance().getObject("springJob");
        return instance;
    }

}
